#!/bin/sh
# PCP QA Test No. 042
# Exercise fixes for some containers issues.
#
# Copyright (c) 2015 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.containers

_check_containers
_check_docker_binary
_check_docker_images busybox

_cleanup()
{
    if [ -n "$container" ]
    then
        echo "== removing container" | tee -a $seq_full
        _remove_docker_containers $container
        container=""
    fi 
    rm -rf $tmp.*
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter_pminfo()
{
    # filter ip_vti tunnel type connections that may exist on (some) machines
    tee -a $seq_full >$tmp.tmp
    sed <$tmp.tmp \
	-e '/ip_vti/d' \
	-e '2q' \
    # end
    sed <$tmp.tmp \
	-e '1,2d' \
	-e 's/\[[0-9][0-9]* or ".*"\]/[INST or NAME]/' \
	-e 's/value [-?0-9.e+][-?0-9.e+]*/value NUMBER/' \
	-e 's/value ".*"$/value STRING/' \
    | LC_COLLATE=POSIX sort \
    | uniq
}

# real QA test starts here
container=`$docker run -d busybox sleep 15`
echo "== container: $container" >> $seq_full

echo "== kernel PMDA (LINUX_NAMESPACE_NET for network interfaces)" | tee -a $seq_full
# expect 2 interfaces (lo/eth0)
metrics="network.interface.in.bytes"
for m in $metrics
do
    pminfo --fetch --container=$container $m | _filter_pminfo
done
echo

if false
then
    # this part does not work as of Feb 2025
    # someone may wish to revisit it at some point in the future
    #
    echo "== procfs PMDA" | tee -a $seq_full
    # expect values for a single process (sleep) and one cgroup
    metrics="proc.memory.rss cgroup.memory.stat.pgfault"
    for m in $metrics
    do
	pminfo --fetch --container=$container $m | _filter_pminfo
    done
    echo
fi

echo "== pmcd PMDA (LINUX_NAMESPACE_UTS for uname)" | tee -a $seq_full
# expect a different hostname to local hostname
pmprobe --values --container=$container pmcd.hostname > $tmp.chost
pmprobe --values pmcd.hostname > $tmp.host
container_hostname=`awk '{ print $3 }' $tmp.chost`
localhost_hostname=`awk '{ print $3 }' $tmp.host`

cat $tmp.chost $tmp.host >> $seq_full
echo container hostname: $container_hostname >> $seq_full
echo localhost hostname: $localhost_hostname >> $seq_full

if [ "$container_hostname" != "$localhost_hostname" ]
then
    echo
    echo "OK: host and container names are different"
    echo
else
    echo "FAIL: hostnames match when they should not"
    echo "localhost: $localhost_hostname"
    echo "container: $container_hostname"
    status=1
    exit
fi

echo "== kernel PMDA (LINUX_NAMESPACE_MNT for filesystem mounts)" | tee -a $seq_full
# expect a different mount device for "/"
pminfo --fetch --container=$container filesys.mountdir > $tmp.chost
pminfo --fetch filesys.mountdir > $tmp.host
container_rootdev=`awk '$NF == "\"/\"" { print $4 }' $tmp.chost`
localhost_rootdev=`awk '$NF == "\"/\"" { print $4 }' $tmp.host`

cat $tmp.chost $tmp.host >> $seq_full
echo container root dev: $container_rootdev >> $seq_full
echo localhost root dev: $localhost_rootdev >> $seq_full

if [ "$container_rootdev" != "$localhost_rootdev" ]
then
    echo
    echo "OK: host and container mount device for / are different"
    echo
else
    echo "FAIL: mount device for / match when they should not"
    echo "localhost: $localhost_rootdev"
    echo "container: $container_rootdev"
    status=1
    exit
fi

# success, all done
status=0
exit
